import re


pattern = '''(?P<remote>[\d.]+) - - \[(?P<datetime>[\w/: +]+)\] "(?P<method>\w+) (?P<url>\S+) \
(?P<protocol>[\w\d/.]+)" (?P<status>\d+) (?P<length>\d+) "-" "(?P<useragent>.+)" "-"'''
regex1 = re.compile(pattern)

REMOTE_DICT = {}
URL_DICT = {}


def sort(dict_name, reverse=False):
    return sorted(dict_name.items(), key=lambda x: x[1], reverse=reverse)


def extract(regex, line: str, reverse=False, remote_dict=REMOTE_DICT, url_dict=URL_DICT):
    m = regex.match(line)
    if m:
        for k, v in m.groupdict().items():
            if k == 'remote':
                remote_dict[v] = remote_dict.get(v, 0) + 1
            if k == 'url':
                url_dict[v] = url_dict.get(v, 0) + 1
        return sort(remote_dict, reverse=reverse), sort(url_dict, reverse=reverse)
    else:
        raise Exception('NO MATCH IN {}'.format(line))


with open('access.log-20191119') as f:
    lines = f.readlines()
    for line1 in lines:

        try:
            result = extract(regex1, line1, reverse=True)
            remote_dict1, url_dict1 = result
            
        except Exception:
            print(line1, end='')

    print(remote_dict1[:10])
    print(url_dict1[:10])
